package com.woniu.secondhand.controller;

import com.woniu.common.annotation.LoginUser;
import com.woniu.common.entity.Result;
import com.woniu.secondhand.constant.SecondhandMessageConstant;
import com.woniu.secondhand.entity.dto.MessageAddDTO;
import com.woniu.secondhand.service.ISecondhandMessageService;
import com.woniu.user.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HMMT
 * @since 2023-05-20
 */
@RestController
@RequestMapping("/secondhandMessage")
@Api(tags = "消息管理")
@Slf4j
public class SecondhandMessageController {

    @Autowired
    private ISecondhandMessageService messageService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/add")
    @ApiOperation("根据情况保存不同的消息")
    public Result addMessage(@RequestBody MessageAddDTO messageAddDTO, @LoginUser UserLoginVO userLoginVO) {
        boolean flag = messageService.addMessage(messageAddDTO, userLoginVO);
        return Result.commonByFlag(flag);
    }

    @GetMapping("/system/read/{messageId}")
    @ApiOperation("更新系统消息为已读")
    @ResponseBody
    public Result readSystem(@PathVariable String messageId, @LoginUser UserLoginVO userLoginVO) {
        redisTemplate.opsForSet().remove(SecondhandMessageConstant.SYSTEM_MESSAGE_REDIS + messageId, userLoginVO.getId());
        //在系统消息的set集合中删除该用户的id
        return Result.ok();
    }

    @GetMapping("/read/{messageId}")
    @ApiOperation("更新消息（非系统消息）为已读")
    @ResponseBody
    public Result readMessage(@PathVariable String messageId) {
        messageService.readMessage(messageId);
        return Result.ok();
    }

}
